I have a problem with the Label's width. If I change the size of the window and the label's width doesn't fit to the window.
I expect something like:
Initial: 012345678901234567890123456789
After resize: 01234567890123...
But the actual state:
After resize:
How can I get my expected result?
Here is the .fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.labeltest.Controller">
<HBox>
<Label fx:id="label"/>
</HBox>
</AnchorPane>
asked May 24, 2018 at 10:09
Sunflame
3,1965 gold badges27 silver badges57 bronze badges
1 Answer 1
An AnchorPane does not resize a child without constraints. You need to set the rightAnchor and leftAnchor constraints of the HBox. (You could also simply use the HBox as root.)
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.labeltest.Controller">
<children>
<HBox AnchorPane.rightAnchor="0" AnchorPane.leftAnchor="0">
<children>
<Label fx:id="label"/>
</children>
</HBox>
</children>
</AnchorPane>
answered May 24, 2018 at 10:25
fabian
82.9k12 gold badges99 silver badges124 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Sunflame
Yes you are right I am using simply the
HBox as root and it works :) Thanks.lang-java
HBoxis probably still the same size, asAnchorPanedoesn't resize its children (I think). Try removing theAnchorPane, having theHBoxas the root.